package tobsn.locationcalendar.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapter {

	private Context context;
	private SQLiteDatabase database;
	private DBOpenHelper dbHelper;

	public DBAdapter(Context context) {
		this.context = context;
	}

	public DBAdapter open() throws SQLException {
		dbHelper = new DBOpenHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Create a new notification. If the notification is successfully created
	 * return the new rowId for that note, otherwise return a -1 to indicate
	 * failure.
	 */
	public long createPON(String text, int latitude, int longitude) {
		ContentValues initialValues = createContentValues(text, latitude,
				longitude);

		return database.insert(DBOpenHelper.NOTIFICATION_TABLE_NAME, null,
				initialValues);
	}

	/**
	 * Update the notification with given rowId.
	 */
	public boolean updatePON(long rowId, String text, int latitude,
			int longitude) {
		ContentValues updateValues = createContentValues(text, latitude,
				longitude);

		return database.update(DBOpenHelper.NOTIFICATION_TABLE_NAME,
				updateValues, DBOpenHelper.ROW_ID + "=" + rowId, null) > 0;
	}

	public boolean updatePON(int rowId, String text) {
		ContentValues values = new ContentValues();
		values.put(DBOpenHelper.NOTIFICATION_COLUMN_TEXT, text);

		return database.update(DBOpenHelper.NOTIFICATION_TABLE_NAME, values,
				DBOpenHelper.ROW_ID + "=" + rowId, null) > 0;
	}

	/**
	 * Deletes the notification with given rowId.
	 */
	public boolean deletePON(long rowId) {
		return database.delete(DBOpenHelper.NOTIFICATION_TABLE_NAME,
				DBOpenHelper.ROW_ID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all notifications in the database
	 * 
	 * @return Cursor over all notifications
	 */
	public Cursor fetchAllPON() {
		return database.query(DBOpenHelper.NOTIFICATION_TABLE_NAME,
				new String[] { DBOpenHelper.ROW_ID,
						DBOpenHelper.NOTIFICATION_COLUMN_TEXT,
						DBOpenHelper.NOTIFICATION_COLUMN_LATITUDE,
						DBOpenHelper.NOTIFICATION_COLUMN_LONGITUDE }, null,
				null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the defined notfication
	 */
	public Cursor fetchPON(long rowId) throws SQLException {
		Cursor mCursor = database
				.query(true, DBOpenHelper.NOTIFICATION_TABLE_NAME,
						new String[] { DBOpenHelper.ROW_ID,
								DBOpenHelper.NOTIFICATION_COLUMN_TEXT,
								DBOpenHelper.NOTIFICATION_COLUMN_LATITUDE,
								DBOpenHelper.NOTIFICATION_COLUMN_LONGITUDE },
						DBOpenHelper.ROW_ID + "=" + rowId, null, null, null,
						null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	private ContentValues createContentValues(String text, int latitude,
			int longitude) {
		ContentValues values = new ContentValues();
		values.put(DBOpenHelper.NOTIFICATION_COLUMN_TEXT, text);
		values.put(DBOpenHelper.NOTIFICATION_COLUMN_LATITUDE, latitude);
		values.put(DBOpenHelper.NOTIFICATION_COLUMN_LONGITUDE, longitude);
		return values;
	}
}
